<template>
  <my-sign-in2 :ipaddr.sync="ipaddr" :try-sign-in="trySignIn" v-show="!connect"></my-sign-in2>
  <button type="button" class="btn btn-default" @click.prevent="onDisconnect" v-show="connect">{{$t("common.disconnect")}}</button>
</template>

<script>
import _ from 'lodash'
import async from 'async'
import ebus from 'libs/ebus.js'
import toast from 'libs/toast.js'
import {setHostname, http} from 'share/server/api.js'
import {encryptPassword} from 'share/server/utils.js'
import {getPlatformInfo, tryLogin} from 'share/server/vuex/actions/system.js'
import {doLoadTopoSummary} from 'share/server/vuex/actions/devmng.js'
import debugCfg from 'share/debugCfg.js'

export default {
  vuex: {
    actions: {
      getPlatformInfo,
      tryLogin,
      doLoadTopoSummary
    }
  },
  data () {
    return {
      connect: false,
      ipaddr: ''
    }
  },
  methods: {
    encryptPassword (plaintext) {
      //兼容旧版本，待废弃
      if (plaintext === 'admin') {
        return plaintext
      } else {
        return encryptPassword(plaintext)
      }   
    },
    stepGetPlatformInfo (verify, cb) {
      let ctx = this
      this.getPlatformInfo({
        success () {
          cb(null, verify)
        },
        error (data) {
          toast.code(ctx, data)
          cb('error')
        },
        noResponse () {
          toast.e(ctx, ctx.$t('common.fail'), ctx.$t('error.noResponse'))
          cb('error')
        }
      })
    },
    stepLogin (verify, cb) {
      if (verify) {
        let ctx = this
        let req = {
          username: verify.username,
          password: this.encryptPassword(verify.password),
          code: ''
        }

        this.tryLogin(req, {
          success () {
            cb(null)
          },
          error (data) {
            toast.code(ctx, data)
            cb('error')
          },
          noResponse () {
            toast.e(ctx, ctx.$t('common.fail'), ctx.$t('error.noResponse'))
            cb('error')
          }
        })        
      } else {
        http('/api/auth/settings', 'GET').then(function(res) {
          if (res && res.data && res.data.code === 0) {
            cb(null)
          } else {
            cb('error')
          }
        }, function(res) {
          cb('error')
        })  
      }     
    },
    stepLoadTopoSummary (cb) {
      let ctx = this
      this.doLoadTopoSummary({
        cb: {
          success () {
            cb(null)
          },
          error (data) {
            toast.code(ctx, data)
            cb('error')
          },
          noResponse () {
            toast.e(ctx, ctx.$t('common.fail'), ctx.$t('error.noResponse'))
            cb('error')
          }
        }
      })
    },
    trySignIn (verify) {
      setHostname(this.ipaddr)

      async.waterfall([
        function(cb) {
          cb(null, verify)      
        }, 
        this.stepGetPlatformInfo,
        this.stepLogin,
        this.stepLoadTopoSummary,
        ], 
        function (err) {
          if (!err) {
            this.connect = true
            this.$emit('ev-connected')
          }
        }.bind(this)
      )
    },
    onDisconnected () {
      this.connect = false
      this.$emit('ev-disconnected')
    },
    onDisconnect () {
      http('/api/auth/logout', 'POST')
      this.onDisconnected()
    },
    loseConnection () {
      this.onDisconnected()
      toast.w(this, this.$t('alert.loseConnection', {msg: ''}), this.$t('alert.plzReSignIn'))
    }
  },
  ready () {
    ebus.on('lose-connection', this.loseConnection)
    if (debugCfg.debugOn) {
      this.ipaddr = debugCfg.ctServerIp
    } else {
      this.ipaddr = window.location.hostname
    }
    this.trySignIn()  
  }
}
</script>

<style>
</style>